package Controller.CoreServlet.Ultility;

import Model.ObjectBean.ViewBean.CatalogItem;
import Model.ObjectBean.ViewBean.ItemOrder;
import java.util.*;
import Model.BeanUltility.*;
import java.sql.ResultSet;
import Model.DataUltility.*;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/** A shopping cart data structure used to track orders.
 *  The OrderPage servlet associates one of these carts
 *  with each user session.
 *
 */
 
public class ShoppingCart {
  private ArrayList itemsOrdered;

  /** Builds an empty shopping cart. */
  
  public ShoppingCart() {
    itemsOrdered = new ArrayList();
  }

  /** Returns List of ItemOrder entries giving
   *  Item and number ordered. Declared as List instead
   *  of ArrayList so that underlying implementation
   *  can be changed later.
   */
  
  public List getItemsOrdered() {
    return(itemsOrdered);
  }

  /** Looks through cart to see if it already contains
   *  an order entry corresponding to item ID. If it does,
   *  increments the number ordered. If not, looks up
   *  Id in database to get this item informations to CatalogItem 
   * and new a itemOrder and add it to itemOrdered
   */
  
  public synchronized void addItem(String itemID) {
    ItemOrder order;
    for(int i=0; i<itemsOrdered.size(); i++) {
      order = (ItemOrder)itemsOrdered.get(i);
      if (order.getItem_ID().equals(itemID)) {
        order.IncreNumItems();
        return;
      }
    }
    Data_Access access = new Data_Access();
    CatalogItem catalog = new CatalogItem();
    
    String []column={"re_id"};
    String []data={itemID};
    ResultSet rs = access.getInformation("resource", column, data); 
        try {
            ResultsetMapper.mapperResultsettoObject(rs, CatalogItem.class, catalog);
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        }
    ItemOrder newOrder = new ItemOrder(catalog);
    itemsOrdered.add(newOrder);
  }

  /** Looks through cart to find order entry corresponding
   *  to item ID listed. If the designated number
   *  is positive, sets it. If designated number is 0
   *  (or, negative due to a user input error), deletes
   *  item from cart.
   */
  
  public synchronized void setNumOrdered(String itemID,
                                         int numOrdered) {
    ItemOrder order;
    for(int i=0; i<itemsOrdered.size(); i++) {
      order = (ItemOrder)itemsOrdered.get(i);
      if (order.getItem_ID().equals(itemID)) {
        if (numOrdered <= 0) {
          itemsOrdered.remove(i);
        } else {
          order.setNumberItems(numOrdered);
        }
        return;
      }
    }
    Data_Access access = new Data_Access();
    CatalogItem catalog = new CatalogItem();
    
    String []column={"re_id"};
    String []data={itemID};
    ResultSet rs = access.getInformation("resource", column, data); 
        try {
            ResultsetMapper.mapperResultsettoObject(rs, CatalogItem.class, catalog);
        } catch (SQLException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(ShoppingCart.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    ItemOrder newOrder = new ItemOrder(catalog);
    itemsOrdered.add(newOrder);
  }
}
    
